﻿using NUnit.Core;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;
using ResultState=QuickTest.Core.TestRunner.ResultState;
using TestResult=QuickTest.Core.TestRunner.TestResult;

namespace QuickTest.Tests.Core.TestRunner
{
    [TestFixture]
    public class NUnitEventListenerTests
    {
        [Test]
        public void Can_Raise_TestFinished()
        {
            TestResult result = null;

            var testEvents = new EventHub();
            testEvents.TestFinished += (sender, e) => result = e.Result;

            var listener = new NUnitEventListener(testEvents);
            listener.TestFinished(new TestCaseResult("MyTestCase"));

            Assert.That(result, Is.Not.Null, "Should return a test case result");
            Assert.That(result.TestName, Is.EqualTo("MyTestCase"), "Should set test case name");
            Assert.That(result.ResultState, Is.EqualTo(ResultState.NotRun), "Should set result state");
        }

        [Test]
        public void Can_Raise_RunStarted()
        {
            int testCount = 0;

            var testEvents = new EventHub();
            testEvents.RunStarted += (sender, e) => testCount = e.TestCount;

            var listener = new NUnitEventListener(testEvents);
            listener.RunStarted("MyTestSuite", 5);

            Assert.That(testCount, Is.EqualTo(5), "Should return test count");
        }

        [Test]
        public void Can_Raise_RunFinished()
        {
            TestRunFinishedEventArgs result = null;

            var testEvents = new EventHub();
            testEvents.RunFinished += (sender, e) => result = e;

            var listener = new NUnitEventListener(testEvents);
            listener.RunFinished(new TestSuiteResult("MyTestSuite"));

            Assert.That(result, Is.Not.Null, "Should return test suite result");
        }

        [Test]
        public void Can_Set_Test_NotRun()
        {
            TestResult result = null;

            var testEvents = new EventHub();
            testEvents.TestFinished += (sender, e) => result = e.Result;

            var listener = new NUnitEventListener(testEvents);
            listener.TestFinished(new TestCaseResult("MyTestCase") {RunState = RunState.Ignored});

            Assert.That(!result.Executed, "Should set result not executed");
        }
    }
}